var Prize = require('../models/Prize');

exports.query = function(req,res){
	var event_id = req.query.event_id;
	var user_id = req.query.user_id;

	Prize.findOne({event_id:event_id,user_id:user_id},function(err,prize){
		if(err){
			res.json({code:0,data:{},info:'系统繁忙，请稍后再试'})
		}else{
			if(prize){
				res.json({code:1,data:prize,info:'返回数据'})
			}else{
				res.json({code:0,data:{},info:'没有中奖信息'})
			}
		}
	})
}

exports.queryAll = function(req,res){
	Prize.find({state:2})
		.populate('user_id')
		.exec(function(err,prizes){
			res.render('prize',{
				prizes:prizes
			})
		})
}


exports.queryAsyncAll = function(req,res){
	Prize.find({state:2})
		.populate('user_id')
		.exec(function(err,prizes){
			res.json(prizes)
		})
}

exports.ensureUse = function(req,res){
	var prize_id = req.body.prize_id;
	console.log(prize_id)
	Prize.findById(prize_id,function(err,prize){
		console.log(err);
		console.log(prize);
		if(err){
				res.json({code:0})

		}else{
			if(prize){
				prize.use_state = 2;
				prize.save(function(err,$prize){
					if(err){
						res.json({code:0})
					}else{
						res.json({code:1})

					}
				})
			}else{
				res.json({code:0})

			}
		}
	})
}